<?php

namespace app\common\components;

use yii\web\Controller;

class BaseWebController extends Controller {

    public $enableCsrfValidation = false;
    public $layout = false;

    public function get($key, $default_value = "") {
        return \Yii::$app->request->get($key, $default_value);
    }

    public function post($key, $default_value = "") {
        return \Yii::$app->request->post($key, $default_value);
    }

    public function setCookie($name, $value, $expire) {
        $cookies = \Yii::$app->response->cookies;
        $cookies->add(new \yii\web\Cookie([
            'name' => $name,
            'value' => $value,
            'expire' => $expire
        ]));
    }

    public function getCookie($name, $default_val) {
        return \Yii::$app->request->cookies->getValue($name, $default_val);
    }

    public function removeCookie($name) {
        return \Yii::$app->response->cookies->remove($name);
    }

    public function renderJson($data = [], $msg = "ok", $code = 1) {
        header("Content-type: application/json");
        echo json_encode([
            "code" => $code,
            "msg" => $msg,
            "data" => $data,
            "req_id" => uniqid()
        ]);
    }
}